package org.micode.demo.biz.service.impl;

import org.micode.common.helper.I18nMsgHelper;
import org.micode.common.utils.ObjectUtils;
import org.micode.demo.db.cache.config.ISysConfigCacheHandler;
import org.micode.demo.biz.service.BizSysConfigService;
import org.micode.demo.db.cache.config.SysConfigCache;
import org.micode.demo.db.cache.user.SysUserCache;
import org.micode.demo.db.domain.dto.SysConfigDTO;
import org.micode.demo.db.service.DaoSysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.micode.libs.cache.imagecode.ImageCodeCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务，登录、保存个人信息等
 */
@Service
@Slf4j
public class BizSysConfigServiceImpl implements BizSysConfigService {

    @Autowired
    private DaoSysConfigService daoSysConfigService;

    @Autowired
    private ISysConfigCacheHandler sysConfigCacheHandler;

    @Override
    public String getValueByKey(String type, String key, String defaultValue) {
        SysConfigCache configCache = sysConfigCacheHandler.getByKey(SysConfigCache.configKey(type, key));
        if (null == configCache) {
            log.warn(
                    I18nMsgHelper.get("warn.BizSysConfigServiceImpl-getValueByKey.none-fail") + "(type={},key={})",
                    type, key);
            return defaultValue;
        }
        try {
            return configCache.getParamValue();
        } catch (Exception e) {
            log.warn(
                    I18nMsgHelper.get("warn.BizSysConfigServiceImpl-getValueByKey.get-value-fail") + ": type={},key={}",
                    type, key);
            return defaultValue;
        }
    }

    private long longConfigValue(String value, long defaultValue) {
        try {
            long ret = Long.parseLong(value);
            if (ret <= 0) {
                ret = defaultValue;
            }
            return ret;
        } catch (Exception e) {
            return defaultValue;
        }
    }

    @Override
    public void reloadConfig() {
        loadSysConfigCache();
    }

    @Override
    public void loadSysConfigCache() {
        List<SysConfigDTO> list = daoSysConfigService.loadAllList();
        if (null != list) {
            list.forEach(item -> {
                SysConfigCache sysConfig = new SysConfigCache() ;
                ObjectUtils.copyProperties(item,sysConfig);
                sysConfigCacheHandler.setByKey(
                        SysConfigCache.configKey(item.getParamType(), item.getParamKey()),
                        sysConfig);
            });
        }
    }

    @Override
    public long getUserTokenRetentionSeconds() {
        return longConfigValue(
                getValueByKey("cache.retention.seconds", "user.token", ""),
                SysUserCache.DEFAULT_RETENTION_SECONDS);
    }

    @Override
    public String defaultAdminLoginName() {
        return getValueByKey("platform", "sys.admin.loginname", "Admin1234");
    }

    @Override
    public String defaultAdminPassword() {
        return getValueByKey("platform", "sys.admin.password", "Admin1234");
    }

    @Override
    public String defaultUserPassword() {
        return getValueByKey("platform", "sys.user.password", "User1234");
    }

    @Override
    public int loginNumWhenLockUser() {
        return Integer.parseInt(getValueByKey("platform", "sys.user.lock.loginnum", "50"));
    }

    @Override
    public long getImageCodeRetentionSeconds() {
        long defaultValue = ImageCodeCache.DEFAULT_RETENTION_SECONDS;
        return longConfigValue(getValueByKey("cache.retention.seconds", "image.code", ""), defaultValue);
    }

}
